package com.mryan.ps_common.handle;

import com.mryan.ps_common.entity.PSResponse;
import com.mryan.ps_common.exception.PSAuthException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @BelongsProject: ps_cloud
 * @BelongsPackage: com.mryan.ps_common.handle
 * @Author: Yan
 * @CreateTime: 2021-04-25 21:32
 * @Description: 全局异常处理器
 */
@Slf4j
public class BaseExceptionHandle {


	@ExceptionHandler(value = Exception.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public PSResponse handleException(Exception e) {
		log.error("系统内部异常，异常信息", e);
		return new PSResponse().message("系统内部异常");
	}

	@ExceptionHandler(value = PSAuthException.class)
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public PSResponse handleFebsAuthException(PSAuthException e) {
		log.error("系统错误", e);
		return new PSResponse().message(e.getMessage());
	}

	@ExceptionHandler(value = AccessDeniedException.class)
	@ResponseStatus(HttpStatus.FORBIDDEN)
	public PSResponse handleAccessDeniedException(){
		return new PSResponse().message("没有权限访问该资源");
	}
}
